package com.demo0211;

import java.util.Arrays;

/**
 * Created by cc
 * Description:
 * User: CZH
 * Date: 2023-02-11
 * Time: 9:53
 */
public class Test01 {

    public static void main15(String[] args) {
        int[][] arr1 = new int[2][];
        int[][] arr2 = {{1,2,3}, {4,5,6}, {7,8,9}};
        for (int[] arr3 : arr2) {
            for (int arr4 : arr3) {
                System.out.print(arr4 + " ");
            }
            System.out.println();
        }
    }

    public static void main14(String[] args) {
        int[] arr1 = {1,1,5,9,7,64,4,34,8,10};
        transform9(arr1);
        printArray(arr1);
    }
    public static void transform9(int[] arr) {
        int left = 0; //找奇数
        int right = arr.length-1; //找偶数
        while (left <= right) {
            if ((arr[left] % 2 == 1) && (arr[right] % 2 == 0)) {
                int tmp = arr[left];
                arr[left] = arr[right];
                arr[right] = tmp;
            }else if (arr[left] % 2 != 1) {
                left++;
            }else if (arr[right] % 2 != 0) {
                right--;
            }
        }
    }

    public static void main13(String[] args) {
        int[] arr1 = {1,2,3,4,5};
        reverse(arr1);
        printArray(arr1);
    }
    public static void reverse(int[] arr) {
        int left = 0;
        int right = arr.length-1;
        while (left <= right) {
            int tmp = arr[left];
            arr[left] = arr[right];
            arr[right] = tmp;
            left++;
            right--;
        }
    }

    public static void main12(String[] args) {
        int[] arr1 = {1,9,3,7,8,4,2,6};
        int[] arr2 = {1,9,3,7,8,4,2,6};
        bubbleSort(arr1);
        printArray(arr1);
        Arrays.sort(arr2);
        printArray(arr2);

    }
    public static void bubbleSort(int[] arr) {
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (arr[j] > arr[j+1]) {
                    int tmp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = tmp;
                }
            }
        }
    }

    public static void main11(String[] args) {
        int[] arr1 = {1,2,1,4,5,6};
        System.out.println(binarySearch(arr1, 3));
        System.out.println(isSorted(arr1));
    }
    public static boolean isSorted(int[] arr) {
        boolean compare = arr[0] > arr[1];  // 判断是否有序
        if (compare) {
            for (int i = 0; i < arr.length - 1; i++) {
                // 从大到小
                if (arr[i] < arr[i + 1]) {
                    return false;
                }
            }
        }else {
            for (int i = 0; i < arr.length - 1; i++) {
                // 从小到大
                if (arr[i] > arr[i+1]) {
                    return false;
                }
            }
        }
        return true;
    }
    public static int binarySearch(int[] arr, int num) {
        int left = 0;
        int mid = 0;
        int right = arr.length-1;
        while (left <= right) {
            mid = (left + right) / 2;
            if (arr[mid] > num) {
                right = mid - 1;
            } else if (arr[mid] < num) {
                left = mid + 1;
            } else {
                return mid + 1;
            }
        }
        return -1;
    }

    public static void main10(String[] args) {
        int[] arr = {1,2,3,4,5};
        int[] ints = my_copyOf(arr, arr.length);
        printArray(ints);
    }
    public static int[] my_copyOf(int[] arr, int len) {
        int[] newArr = new int[len];
        for (int i = 0; i < len; i++) {
            newArr[i] = arr[i];
        }
        return newArr;
    }

    public static void main9(String[] args) {
        int[] arr1 = {1,2,3,4,5};
        int[] arr2 = Arrays.copyOf(arr1, arr1.length);
        printArray(arr2);
        int[] arr3 = Arrays.copyOfRange(arr1, 0, arr1.length-1);
        printArray(arr3);
        int[] arr4 = arr1;
        printArray(arr4);
        arr4[0] = 111;
        printArray(arr4);
        printArray(arr1);
    }

    public static void main8(String[] args) {
        int[] arr1 = {1,2,3,4,5,6};
        System.out.println(Arrays.toString(arr1));
//        System.out.println(Arrays.toString(args));
        System.out.println(myToString(arr1));
    }
    public static String myToString(int[] arr) {
        String str = "[";
        for (int i = 0; i < arr.length-1; i++) {
            str += arr[i] + ", ";
        }
        str += (arr[arr.length-1] + "]");
        return str;
    }

    public static void main7(String[] args) {
        int[] arr1 = new int[]{1, 2, 3};
        int[] arr2 = {1, 2, 3};
        printArray(arr1);
        transformtwo(arr1);
        printArray(arr1);
        int[] transform = transform(arr2);
        printArray(transform);
    }
    public static int[] transform(int[] arr) {
        int[] tmp = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            tmp[i] = arr[i] * 2;
        }
        return tmp;
    }
    public static void transformtwo(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            arr[i] *= 2;
        }
    }
    public static void printArray(int[] arr) {
        for (int n : arr) {
            System.out.print(n + " ");
        }
        System.out.println();
    }


    public static void main6(String[] args) {
        System.out.println(fib1(40));
        System.out.println(fib2(40));
    }
    public static int fib2(int n) {
        if (n < 3) {
            return 1;
        }
        int tmp = 0;
        int last1 = 1;
        int last2 = 1;
        for (int i=3; i<=n; i++) {
            tmp = last1 + last2;
            last1 = last2;
            last2 = tmp;
        }
        return last2;
    }
    public static int fib1(int n) { //1 1 2 3 5 8 13
        if (n < 3) {
            return 1;
        }
        return fib1(n - 1) + fib1(n - 2);
    }

    public static void main5(String[] args) {
        System.out.println(sumNum1(1729));
    }
    public static int sumNum1(int n) { //1 7 2 9   19
        if (n < 10) {
            return n;
        }
        return (n % 10) + sumNum1(n / 10);
    }

    public static void main4(String[] args) {
        System.out.println(sumNum(10));
    }
    public static int sumNum(int n) {
        if (n == 1) {
            return n;
        }
        return n + sumNum(n-1);
    }

    public static void main3(String[] args) {
        print(123);
    }
    public static void print(int n) { //123
        if (n > 9) {
            print(n / 10);
        }
        System.out.println(n % 10);
    }

    public static void main2(String[] args) {
        System.out.println(factor(5));
    }
    public static int factor(int n) {
        if (n < 3) {
            return n;
        }
        return n * factor(n-1);
    }

    public static void main1(String[] args) {
        int[] arr = new int[]{1,2};
        swap(arr);
        for (int n : arr) {
            System.out.println(n);
        }
    }
    public static void swap(int[] arr) {
        int tmp = arr[0];
        arr[0] = arr[1];
        arr[1] = tmp;
    }
    public static double add(double x, double y) {
        return x + y;
    }
    public static int add(int x, int y, int z) {
        return x + z + y;
    }
    public static int add(int x, int y) {
        return x + y;
    }
}
